'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.Linq
Imports System.Data.Linq

Public Class XmlIdentityProvider
    Inherits IdentityProvider

    Private _xmlFile As System.IO.FileInfo

    Public Sub New(ByVal xmlFile As System.IO.FileInfo)
        _xmlFile = xmlFile
    End Sub

    Private Function CreateDataSet() As DataSet
        Dim ds As DataSet
        Dim dtIdentities As DataTable
        Dim dtAttributes As DataTable

        ds = New DataSet("IdentityData")
        dtIdentities = ds.Tables.Add("Identities")
        dtIdentities.Columns.Add("Id", GetType(String))
        dtIdentities.Columns.Add("Identifier", GetType(String))
        dtIdentities.Columns.Add("ImageUrl", GetType(String))
        dtIdentities.Columns.Add("Description", GetType(String))

        dtAttributes = ds.Tables.Add("Attributes")
        dtAttributes.Columns.Add("Id", GetType(String))
        dtAttributes.Columns.Add("IdentityId", GetType(String))
        dtAttributes.Columns.Add("Key", GetType(String))
        dtAttributes.Columns.Add("Value", GetType(Object))

        Return ds
    End Function

    Public Sub Load()
        ' if no file exists, exit :)
        If Not _xmlFile.Exists Then Return

        Dim ds As DataSet

        ' create the dataset schema
        ds = CreateDataSet()

        ' read the data
        ds.ReadXml(_xmlFile.FullName)

        ' query identities
        Dim qTypedIdentities = From x In ds.Tables("Identities").AsEnumerable Select New With _
                                {.Id = x.Field(Of String)("ID"), _
                                 .Identifier = x.Field(Of String)("Identifier"), _
                                 .Description = x.Field(Of String)("Description"), _
                                 .ImageUrl = x.Field(Of String)("ImageUrl")}

        ' query attributes
        Dim qTypedAttributes = From x In ds.Tables("Attributes").AsEnumerable Select New With _
                           {.IdentityId = x.Field(Of String)("IdentityId"), _
                            .Key = x.Field(Of String)("Key"), _
                            .Value = x.Field(Of Object)("Value")}

        Dim qGroupedAttributes = From x In qTypedAttributes Group By x.IdentityId Into Group Select IdentityId, Group

        ' add attributes to identities
        For Each attributeGroup In qGroupedAttributes
            Dim g = attributeGroup
            Dim data = (From x In qTypedIdentities Where x.Id = g.IdentityId).FirstOrDefault
            Dim identity As Identity

            identity = New Identity(data.Identifier)
            identity.Description = data.Description
            identity.ImageUrl = data.ImageUrl

            For Each a In g.Group
                identity.SetAttribute(a.Key, a.Value)
            Next

            _Identities.Add(identity)
        Next
    End Sub

    Public Sub Save()
        Dim ds As DataSet

        If Not _xmlFile.Directory.Exists Then _xmlFile.Directory.Create()

        ' create the dataset schema
        ds = CreateDataSet()

        For Each id In _Identities
            Dim row As DataRow = ds.Tables("Identities").NewRow

            row("Id") = Guid.NewGuid.ToString
            row("Identifier") = id.Identifier
            row("ImageUrl") = id.ImageUrl
            row("Description") = id.Description

            ds.Tables("Identities").Rows.Add(row)

            For Each attributeName In id.GetAttributeNames
                Dim ra As DataRow = ds.Tables("Attributes").NewRow

                ra("Id") = Guid.NewGuid.ToString
                ra("IdentityId") = row("Id")
                ra("Key") = attributeName
                ra("Value") = id.GetAttribute(attributeName)

                ds.Tables("Attributes").Rows.Add(ra)
            Next
        Next

        ds.WriteXml(_xmlFile.FullName)
    End Sub
End Class